package cn.sdhuijin.healthexam.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.bean.User;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 修改密码
 * 
 * @author sunping
 */
public class UpdatePWDActivity extends AbstractActivity implements
		OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pwd);
		ExitApplication.getInstance().addActivity(this);

		this.setButtonListener(this, R.id.btn_back, R.id.btn_save);
	}

	/**
	 * 修改密码
	 */
	private void updatePassword() {
		DatabaseHelper dbh = new DatabaseHelper(UpdatePWDActivity.this);
		String newPasswordFirst = this
				.getControlValue(R.id.edt_new_password_first);
		String newPasswordSecond = this
				.getControlValue(R.id.edt_new_password_second);
		String oldPassword = this.getControlValue(R.id.edt_old_password);
		if (Util.allIsNotEmpty(oldPassword)) {
			if (Util.allIsNotEmpty(newPasswordFirst, newPasswordSecond)) {
				if (newPasswordFirst.equals(newPasswordSecond)) {
					if (dbh.login(User.getCurrentUser().getName(), oldPassword)) {
						dbh.updateUserPassword(User.getCurrentUser().getName(),
								newPasswordFirst);
						this.toastInformation(R.string.update_pwd_success);
					} else {
						this.toastInformation(R.string.old_pwd_error);
					}
				} else {
					this.toastInformation(R.string.new_pwd_not_same);
				}
			} else {
				this.toastInformation(R.string.new_pwd_not_null);
			}
		} else {
			this.toastInformation(R.string.old_pwd_not_null);
		}
	}

	@Override
	public void onClick(View v) {
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.btn_save:
			updatePassword();
			break;
		case R.id.btn_back:
			finish();
			break;
		}
	}
}
